/*************************************************************************
	> File Name: 9.HZOJ-362.cpp
	> Author: hug
	> Mail: hug@haizeix.com
	> Created Time: 五  2/14 14:15:32 2025
 ************************************************************************/

#include <iostream>
using namespace std;

struct Edge {
    int to, nxt;
} e[305];
int head[305], cnt = 0;
void add(int a, int b) {
    e[++cnt] = {b, head[a]};
    head[a] = cnt;
    return ;
}

int n, m;
int sc[305];
int dp[305][305], ns[305];

void getDP(int ind) {
    ns[ind] = 1;
    dp[ind][0] = 0;
    dp[ind][1] = sc[ind];
    for (int p = head[ind]; p; p = e[p].nxt) {
        int to = e[p].to;
        getDP(to);
        ns[ind] += ns[to];
        for (int j = min(m, ns[ind]); j >= 2; j--) {
            for (int k = 1; k <= ns[to]; k++) {
                if (j < k + 1) break;
                dp[ind][j] = max(dp[ind][j], dp[ind][j - k] + dp[to][k]);
            }
        }
    }
    return ;
}

int main() {
    cin >> n >> m;
    for (int i = 1, a; i <= n; i++) {
        cin >> a >> sc[i];
        add(a, i);
    }
    m += 1;
    getDP(0);
    cout << dp[0][m] << endl;
    return 0;
}
